package com.bnzj.cloud.auth.custom;

import com.bnzj.core.web.constant.CommonWebEnum;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;


/**
 * @ClassName CustomEndpointExceptionTranslator
 * @Description 登录失败返回数据适配成平台所需数据格式
 * @Author Joy.zhou
 * @Date 2020/3/12
 * @Version V1.0
 **/
public class CustomEndpointExceptionTranslator extends DefaultWebResponseExceptionTranslator {

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
        ResponseEntity<OAuth2Exception> response = super.translate(e);
        OAuth2Exception auth2Exception = response.getBody();
        auth2Exception.addAdditionalInformation("code", CommonWebEnum.LOGIN_FAILED.getCode()+"");
        auth2Exception.addAdditionalInformation("msg", CommonWebEnum.LOGIN_FAILED.getMsg()+"");
        return new ResponseEntity<OAuth2Exception>(auth2Exception, response.getHeaders(),
                HttpStatus.OK);
    }
}
